#include "Protocol.hpp"
#include "Sock.hpp"


/* // version1 原生方法，没有明显的序列化和反序列化的过程
void *HandlerRequest(void *args)
{
    int sock = *(int *)args;
    delete (int *)args;
    pthread_detach(pthread_self());
    // 业务逻辑, 做一个短服务
    // request -> 分析处理 -> 构建response -> sent(response)->close(sock)
    
    request_t req;
    memset(&req, 0, sizeof(req));
    ssize_t s = read(sock, &req, sizeof(req));

    response_t resp;
    memset(&resp, 0, sizeof(resp));

    if (s == sizeof(req))
    {
        switch (req.op)
        {
        case '+':
            resp.result = req.x + req.y;
            break;
        case '-':
            resp.result = req.x - req.y;
            break;
        case '*':
            resp.result = req.x * req.y;
            break;
        case '/':
            if (req.y == 0)
                resp.code = -1; // div 0
            else
                resp.result = req.x / req.y;
            break;
        case '%':
            if (req.y == 0)
                resp.code = -2; // % 0
            else
                resp.result = req.x % req.y;
            break;
        default:
            resp.code = -3; // 未知错误
            break;
        }
        cout << "request: " << req.x << req.op << req.y << endl;
        write(sock, &resp, sizeof(resp));
    }

    close(sock);
}*/

//version2 序列化和反序列化
void *HandlerRequest(void *args)
{
    int sock = *(int *)args;
    delete (int *)args;
    pthread_detach(pthread_self());

    // request -> 分析处理 -> 构建response -> sent(response)->close(sock)
    

    //1.读取请求
    request_t req;
    memset(&req, 0, sizeof(req));
    char buffer[1024];
    ssize_t s = read(sock, buffer, sizeof(buffer)-1);
    if(s > 0)
    {
        buffer[s] = 0;
        //反序列化
        std::string req_string = buffer;
        DeserializeRequest(req_string, req);
        
        response_t resp;
        memset(&resp, 0, sizeof(resp));

        switch (req.op)
        {
        case '+':
            resp.result = req.x + req.y;
            break;
        case '-':
            resp.result = req.x - req.y;
            break;
        case '*':
            resp.result = req.x * req.y;
            break;
        case '/':
            if (req.y == 0)
                resp.code = -1; // div 0
            else
                resp.result = req.x / req.y;
            break;
        case '%':
            if (req.y == 0)
                resp.code = -2; // % 0
            else
                resp.result = req.x % req.y;
            break;
        default:
            resp.code = -3; // 未知错误
            break;
        }
        cout << "request: " << req.x << req.op << req.y << endl;

        std::string send_string = SerializeResponse(resp);   //序列化之后的字符串
        write(sock, send_string.c_str(),send_string.size());     
        cout << "服务结束: " << send_string << endl;
    }

    close(sock);
}

//./CalServer port
int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        cout << "Usage: \n\t" << argv[0] << "port" << endl;
        exit(1);
    }

    int listen_sock = Sock::Socket();
    Sock::Bind(listen_sock, atoi(argv[1]));
    Sock::Listen(listen_sock);
    for (;;)
    {
        int new_sock = Sock::Accept(listen_sock);
        if (new_sock >= 0)
        {
            cout << "get a new client...  " << new_sock << endl;
            int *pram = new int(new_sock);
            pthread_t tid;
            pthread_create(&tid, nullptr, HandlerRequest, (void *)pram);
        }
    }
}